package com.jsy.user.controller;

import com.github.pagehelper.PageInfo;
import com.jsy.common.constant.StatusCode;
import com.jsy.common.vo.Result;
import com.jsy.user.pojo.Areas;
import com.jsy.user.service.AreasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:www.jsy.com
 * @Description:
 * @Date www.jsy.com
 *****/

@RestController
@RequestMapping("/areas")
@CrossOrigin
public class AreasController {

    @Autowired
    private AreasService areasService;

    /***
     * Areas分页条件搜索实现
     * @param areas: 查询条件
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Areas>> findPage(@RequestBody(required = false) Areas areas, @PathVariable  int page, @PathVariable  int size){
        //调用AreasService实现分页条件查询Areas
        PageInfo<Areas> pageInfo = areasService.findPage(areas, page, size);
        return new Result<PageInfo<Areas>>("查询成功！", pageInfo);
    }

    /***
     * Areas分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Areas>> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用AreasService实现分页查询Areas
        PageInfo<Areas> pageInfo = areasService.findPage(page, size);
        return new Result<PageInfo<Areas>>("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param areas 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/search" )
    public Result<List<Areas>> findList(@RequestBody(required = false) Areas areas){
        //调用AreasService实现条件查询Areas
        List<Areas> list = areasService.findList(areas);
        return new Result<List<Areas>>("查询成功！", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id 主键
     * @return 查询结果
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用AreasService实现根据主键删除
        areasService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功！");
    }

    /***
     * 修改Areas数据
     * @param areas 更新对象
     * @return 操作结果
     */
    @PutMapping
    public Result update(@RequestBody Areas areas){
        //调用AreasService实现修改Areas
        areasService.update(areas);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    /***
     * 新增Areas数据
     * @param areas 新增对象
     * @return 操作结果
     */
    @PostMapping
    public Result add(@RequestBody Areas areas){
        //调用AreasService实现添加Areas
        areasService.add(areas);
        return new Result(true, StatusCode.OK, "添加成功！");
    }

    /***
     * 根据ID查询Areas数据
     * @param id 主键
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<Areas> findById(@PathVariable String id){
        //调用AreasService实现根据主键查询Areas
        Areas areas = areasService.findById(id);
        return new Result<Areas>("查询成功！", areas);
    }

    /***
     * 查询Areas全部数据
     * @return 查询结果
     */
    @GetMapping
    public Result<List<Areas>> findAll(){
        //调用AreasService实现查询所有Areas
        List<Areas> list = areasService.findAll();
        return new Result<List<Areas>>("查询成功！", list);
    }
}
